# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import fields, osv
from openerp.tools.translate import _
import openerp.addons.decimal_precision as dp
import re
#----------------------------------------------------------
# Products
#----------------------------------------------------------

class belber_business(osv.osv):
    _name = 'belber.product.business'
    _description = 'Segment'

    _columns = {
        'name': fields.char('Business', required=True, select=True),
        'description': fields.text('Description'),
    }

class belber_product_segment(osv.osv):
    _name = 'belber.product.segment'
    _description = 'Segment'

    _columns = {
        'name': fields.char('Segment', required=True, select=True),
        'description': fields.text('Description'),
    }

class belber_product_group(osv.osv):
    _name = 'belber.product.group'
    _description = 'Product Group'

    _columns = {
        'name': fields.char('Product Group', required=True, select=True),
        'description': fields.text('Description'),
    }
    
class belber_product_category(osv.osv):
    _name = 'belber.product.category'
    _description = 'Category'

    _columns = {
        'name': fields.char('Category', required=True, select=True),
        'description': fields.text('Description'),
    }
    
class belber_product_function(osv.osv):
    _name = 'belber.product.function'
    _description = 'Function'

    _columns = {
        'name': fields.char('Function', required=True, select=True),
        'description': fields.text('Description'),
    }
    
class belber_product_model(osv.osv):
    _name = 'belber.product.model'
    _description = 'Model Code'

    _columns = {
        'name': fields.char('Model Code', required=True, select=True),
        'description': fields.text('Model Description', required=True),
    }
    
class belber_product_material(osv.osv):
    _name = 'belber.product.material'
    _description = 'Material Code'

    _columns = {
        'name': fields.char('Material Code', required=True, select=True),
        'description': fields.text('Material Description', required=True),
    }
    
class belber_product_colour(osv.osv):
    _name = 'belber.product.colour'
    _description = 'Colour Code'

    _columns = {
        'name': fields.char('Colour Code', required=True, select=True),
        'description': fields.text('Colour Description', required=True),
    }
    
class belber_product_size(osv.osv):
    _name = 'belber.product.size'
    _description = 'Size Code'

    _columns = {
        'name': fields.char('Size Code', required=True, select=True),
        'description': fields.text('Size Description'),
    }

class product_template(osv.osv):
    _inherit = "product.template"

    _columns = {
                
        'belber_product_business_id': fields.many2one('belber.product.business', 'Business'),
        'belber_product_segment_id': fields.many2one('belber.product.segment', 'Segment'),
        'belber_product_group_id': fields.many2one('belber.product.group', 'Product Group'),
        'belber_product_category_id': fields.many2one('belber.product.category', 'Category'),
        'belber_product_function_id': fields.many2one('belber.product.function', 'Function'),
        'belber_year': fields.integer('Year'),
        'belber_launch_season': fields.char('Launch Season'),
        'belber_product_season': fields.char('Product Season (Season code/ CO)'),
        'belber_line': fields.char('Line'),
        'belber_product_model_id': fields.many2one('belber.product.model', 'Model Code'), 
        'belber_product_material_id': fields.many2one('belber.product.material', 'Material Code'),    
        'belber_product_colour_id': fields.many2one('belber.product.colour', 'Colour Code'), 
        'belber_metal_code': fields.char('Metal Parts Code'),
        'belber_metal_colour': fields.char('Metal Parts Colour'),
        'weight_net': fields.float('Weight (Kg)', digits_compute=dp.get_precision('Stock Weight'), help="The net weight in Kg."),
        'belber_dimensions_ch': fields.char('Dimensions (Cm)'),
        'belber_product_size_id': fields.many2one('belber.product.size', 'Size Code'), 
        'belber_price_point': fields.char('Price Point'),
        'belber_rrp': fields.float('RRP USD'),
        'belber_whole_sale_price': fields.float('Whole Sale Price USD'),
        'belber_supplier_ref': fields.char('Supplier Ref. #'),
        'belber_supplier_code': fields.char('Supplier Code'),
        'belber_supplier_name': fields.char('Supplier Name'),
        'belber_supplier_cost_price': fields.float('Supplier Cost Price USD'),
        'weight': fields.float('Weight (cm)', digits_compute=dp.get_precision('Stock Weight'), help="The gross weight in Kg."),
    }

    _defaults = {
        'name': lambda obj, cr, uid, context: '/',
    }

    def create(self, cr, uid, vals, context=None):
        product_model_obj = self.pool.get('belber.product.model')
        product_material_obj = self.pool.get('belber.product.material')
        product_colour_obj = self.pool.get('belber.product.colour')
        belber_product_model = product_model_obj.browse(cr ,uid ,vals['belber_product_model_id']).name
        belber_product_material = product_material_obj.browse(cr, uid, vals['belber_product_material_id']).name
        belber_product_colour = product_colour_obj.browse(cr, uid, vals['belber_product_colour_id']).name
        belber_metal_code = vals['belber_metal_code']
        product_name = belber_product_model + '.' + belber_product_material + '.' + belber_product_colour + '.' + belber_metal_code
        
        vals.update({'name': product_name})
        
        result = super(product_template, self).create(cr, uid, vals, context=context)
        return result

    def write(self, cr, uid, ids, vals, context=None):
        product_name = belber_product_model = belber_product_material = belber_product_colour = belber_metal_code = ''
        product = self.pool.get('product.template').browse(cr, uid, ids[0], context=None)
        product_model_obj = self.pool.get('belber.product.model')
        product_material_obj = self.pool.get('belber.product.material')
        product_colour_obj = self.pool.get('belber.product.colour')
        
        if 'belber_product_model_id' in vals:
            belber_product_model = product_model_obj.browse(cr ,uid ,vals['belber_product_model_id']).name
        else:
            belber_product_model = product.belber_product_model_id.name
        
        if 'belber_product_material_id' in vals:
            belber_product_material = product_material_obj.browse(cr, uid, vals['belber_product_material_id']).name
        else:
            belber_product_material = product.belber_product_material_id.name
            
        if 'belber_product_colour_id' in vals:
            belber_product_colour = product_colour_obj.browse(cr, uid, vals['belber_product_colour_id']).name
        else:
            belber_product_colour = product.belber_product_colour_id.name
        
        if 'belber_metal_code' in vals:
            belber_metal_code = vals['belber_metal_code']
        else:
            belber_metal_code = product.belber_metal_code
            
        product_name = belber_product_model + '.' + belber_product_material + '.' + belber_product_colour + '.' + belber_metal_code
        
        vals.update({'name': product_name})
            
        return super(product_template, self).write(cr, uid, ids, vals, context=context)

product_template()
